package org.itboys.mobile.service;

import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.dao.wallet.SundayWalletRecordMapper;
import org.itboys.mobile.dto.report.SundayReportProductDto;
import org.itboys.mobile.dto.report.SundayReportWalletDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.wallet.SundayWalletRecord;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.util.DatetUtil;
import org.itboys.param.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_后台首页_service
 */
@Service
public class SundayDesktopService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private SundayProductMapper productMapper;
    @Autowired
    private SundayWalletRecordMapper walletRecordMapper;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayReportService reportService ;

    /**
     * 商户后台首页数据
     *
     * 2，当日销量/销售额，
     * 3，销售总量/总销售额
     *
     */
    public Map<String,Object> getDesktopDataByShop(Long shopId){
        Map<String,Object> result = new HashMap<String,Object>();
        Map<String,Object> param = new HashMap<String,Object>();
        Date today_start = DatetUtil.setDateStart(Calendar.getInstance().getTime());
        Date today_end = DatetUtil.setDateEnd(Calendar.getInstance().getTime());
        Integer today_sale_number=0;//当日销量
        Double today_sale_amount=0.00;//当日销售额
        Integer total_sale_number=0;//总销量
        Double total_sale_amount=0.00;//总销售额
        param.put("shopId",shopId);
        param.put("groupType",SundayCommonConstants.report_product_group_type_product);
        param.put("isDeal",SundayCommonConstants.YES);
        List<SundayReportProductDto> reportProductDtos= productMapper.report(param);
        for(SundayReportProductDto reportProductDto:reportProductDtos){
            //总销售额
            total_sale_amount+=reportProductDto.getSaleAmount().doubleValue();
            //总销量
            total_sale_number+=reportProductDto.getSale();
            if(reportProductDto.getCt().getTime()>=today_start.getTime()&&
                    reportProductDto.getCt().getTime()<=today_end.getTime()){
                //今日销售额
                today_sale_amount+=reportProductDto.getSaleAmount().doubleValue();
                //今日销量
                today_sale_number+=reportProductDto.getSale();
            }
        }
        result.put("today_sale_number",today_sale_number);//当日销量
        result.put("today_sale_amount",new BigDecimal(today_sale_amount).setScale(2,BigDecimal.ROUND_HALF_UP).toString());//当日销售额
        result.put("total_sale_number",total_sale_number);//总销量
        result.put("total_sale_amount", new BigDecimal(total_sale_amount).setScale(2,BigDecimal.ROUND_HALF_UP).toString());//总销售额
        return result;

    }


    /**
     *
     * 后台首页数据
     * 2，当日销量/销售额，当日分销佣金，当日商户销量/总销售额。
     * 3，销售总量/总销售额，总分销佣金，商户总销量/总销售额
     *
     */
    public Map<String,Object> getDesktopData(){
        Map<String,Object> result = new HashMap<String,Object>();
        Map<String,Object> param = new HashMap<String,Object>();
        Date today_start = DatetUtil.setDateStart(Calendar.getInstance().getTime());
        Date today_end = DatetUtil.setDateEnd(Calendar.getInstance().getTime());
        Integer today_sale_number=0;//当日销量
        Double today_sale_amount=0.00;//当日销售额
        Integer total_sale_number=0;//总销量
        Double total_sale_amount=0.00;//总销售额

        Integer shop_today_sale_number=0;//门店当日销量
        Double shop_today_sale_amount=0.00;//门店当日销售额
        Integer shop_total_sale_number=0;//门店总销量
        Double shop_total_sale_amount=0.00;//门店总销售额

        Double today_reward=0.00;//当日佣金
        Double total_reward=0.00;//总佣金

        //-----------------销量和销售额度----------
        param.put("groupType",SundayCommonConstants.report_product_group_type_product);
        param.put("isDeal",SundayCommonConstants.YES);

        List<SundayReportProductDto> reportProductDtos= productMapper.report(param);
        for(SundayReportProductDto reportProductDto:reportProductDtos){
            //总销售额
            total_sale_amount+=reportProductDto.getSaleAmount().doubleValue();
            //总销量
            total_sale_number+=reportProductDto.getSale();
            if(reportProductDto.getCt().getTime()>=today_start.getTime()&&
                    reportProductDto.getCt().getTime()<=today_end.getTime()){
                //今日销售额
                today_sale_amount+=reportProductDto.getSaleAmount().doubleValue();
                //今日销量
                today_sale_number+=reportProductDto.getSale();
            }
            if(LongUtil.isNotZreo(reportProductDto.getShopId())){
                //门店总销售额
                shop_total_sale_amount+=reportProductDto.getSaleAmount().doubleValue();
                //门店总销量
                shop_total_sale_number+=reportProductDto.getSale();

                if(reportProductDto.getCt().getTime()>=today_start.getTime()&&
                        reportProductDto.getCt().getTime()<=today_end.getTime()){
                    //门店今日销售额
                    shop_today_sale_amount+=reportProductDto.getSaleAmount().doubleValue();
                    //门店今日销量
                    shop_today_sale_number+=reportProductDto.getSale();
                }
            }
        }
        //佣金
        param.clear();
        param.put("type",SundayCommonConstants.wallet_record_type_amount_commission);
        List<SundayWalletRecord> walletRecords= walletRecordMapper.select(param);
        for(SundayWalletRecord walletRecord:walletRecords){
            //总佣金
            total_reward+=walletRecord.getAmount().doubleValue();
            if(walletRecord.getCt().getTime()>=today_start.getTime()&&
                    walletRecord.getCt().getTime()<=today_end.getTime()){
                //今日佣金
                today_reward+=walletRecord.getAmount().doubleValue();
            }
        }
        result.put("today_sale_number",today_sale_number);//当日销量
        result.put("today_sale_amount",new BigDecimal(today_sale_amount).setScale(2,BigDecimal.ROUND_HALF_UP).toString());//当日销售额
        result.put("total_sale_number",total_sale_number);//总销量
        result.put("total_sale_amount", new BigDecimal(total_sale_amount).setScale(2,BigDecimal.ROUND_HALF_UP).toString());//总销售额
        result.put("shop_today_sale_number",shop_today_sale_number);//门店当日销量
        result.put("shop_today_sale_amount", new BigDecimal(shop_today_sale_amount).setScale(2,BigDecimal.ROUND_HALF_UP).toString());//门店当日销售额
        result.put("shop_total_sale_number",shop_total_sale_number);//门店总销量
        result.put("shop_total_sale_amount", new BigDecimal(shop_total_sale_amount).setScale(2,BigDecimal.ROUND_HALF_UP).toString());//门店总销售额
        result.put("today_reward", new BigDecimal(today_reward).setScale(2,BigDecimal.ROUND_HALF_UP).toString());//当日佣金
        result.put("total_reward", new BigDecimal(total_reward).setScale(2,BigDecimal.ROUND_HALF_UP).toString());//总佣金




        return result;

    }

    /**
     * 获取今日前10数据
     * @param type 1商品销售前10，2门店销售前10，3分销佣金前10
     * @return
     */
    public PageResult getTodayTopTen(Integer type){
        List results = new ArrayList<>();
        Map<String,Object> param = new HashMap<String,Object>();
        Date today_start = DatetUtil.setDateStart(Calendar.getInstance().getTime());
        Date today_end = DatetUtil.setDateEnd(Calendar.getInstance().getTime());
        if(type == 1 || type ==2){
            param.put("ct_start", TimeUtils.formatToTime(today_start));
            param.put("ct_end",TimeUtils.formatToTime(today_end));
            if(type==1){
                param.put("groupType",SundayCommonConstants.report_product_group_type_product);
            }
            if(type==2){
                param.put("isShop",1);
                param.put("groupType",SundayCommonConstants.report_product_group_type_shop);
            }
            param.put("isDeal",SundayCommonConstants.YES);
            param.put("row_start",0);
            param.put("row_size",10);
            results= productMapper.report(param);
        }else if(type == 3){
            return reportService.getRewardReportData(null,true,null,null);
        }
        return new PageResult(results,Long.valueOf(results.size()));

    }



}
